package com.yonyou.print.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yonyou.print.params.ParamsEngine;
import com.yonyou.service.PrintDBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by yijx on 2018/1/5.
 */
@Controller
public class PrintServiceCtrl {
    @Autowired
    private PrintDBService printDBService;

    //从本地数据库取打印模型
    @RequestMapping(value = "getTemplateModel", method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getTemplateModel(@RequestParam(value = "templateCode") String templateCode) {
        JSONObject templateModel = (JSONObject) JSONObject.toJSON(printDBService.getTemplateModel(templateCode));
        return templateModel.toJSONString();
    }

    //查询本地所有模板
    @RequestMapping(value = "getTemplates", method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getTemplates(@RequestParam(name = "bo_code", defaultValue = "") String bo_code) {
        JSONArray templates = (JSONArray) JSONArray.toJSON(printDBService.getAllTemplates(bo_code));
        return templates.toJSONString();
    }

    @RequestMapping(value = "getPreviewUrl",method = RequestMethod.POST,produces = "application/json;charset=UTF8")
    @ResponseBody
    public String getPreviewUrl(@RequestParam("templateCode") String templateCode,
                                @RequestParam("dataUrl") String dataUrl,@RequestParam("params") String params){
        String tenantId = ParamsEngine.getInfoConfig().getProperty("tenantId");
        String printServer =  ParamsEngine.getInfoConfig().getProperty("serviceUrl");

        JSONObject returnObj = new JSONObject();
        returnObj.put("url","http://" + printServer + "/print_service/print/preview?tenantId=" +
                tenantId + "&template=" + templateCode + "&printcode=" + templateCode +
                "&serverUrl=http://znzzurl/" + dataUrl + "&params=" + params + "&sendType=2");
        return returnObj.toString();
    }
}
